using System;
using System.Collections.Generic;
using System.Text;
using UnitTests.Tort.Objects;
using uk.co.tonyditchfield.tort.persistence;
using uk.co.tonyditchfield.tort.attributes;
using uk.co.tonyditchfield.tort.exception;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.EnterpriseLibrary.Logging;

using System.Reflection;

namespace UnitTests.Tort.DataAccess
{
    [TestClass]
    public class GeneralTesting
    {
        private TestContext testContextInstance;

        
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [DeploymentItem("tort.dll")]
        [TestMethod()]
        public void SaveTest()
        {
            Random rnd = new Random();

            using (IDBTransactionScope scope = new DBTransactionScope("db1"))
            {
                try
                {
                    Address addr = new Address();
                    addr.Address1 = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    addr.Address2 = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    scope.Save(addr);

                    User user = new User();
                    user.UserName = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    user.FirstName = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    user.SomeOtherStuff = "ererwer";
                    user.DateField = DateTime.Now;
                    user.Address = addr;
                    scope.Save(user);

                    addr = new Address();
                    addr.Address1 = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    addr.Address2 = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    scope.Save(addr);

                    user = new User();
                    user.UserName = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    user.FirstName = rnd.Next().ToString(System.Globalization.CultureInfo.CurrentCulture);
                    user.SomeOtherStuff = "ererwer";
                    user.DateField = DateTime.Now;
                    user.Address = addr;
                    scope.Save(user);
                    
                    scope.Complete();
                }
                catch (uk.co.tonyditchfield.tort.exception.PersistenceException pe)
                {
                    scope.Rollback();
                    Assert.Fail(pe.Message);
                }
            }
        }

        [DeploymentItem("tort.dll")]
        [TestMethod()]
        public void LoadByPKTest()
        {
            using (IDBTransactionScope scope = new DBTransactionScope("db1"))
            {
                try
                {
                    scope.LoadByPK(typeof(User), 1000);

                    scope.Complete();
                }
                catch (PersistenceException pe)
                {
                    scope.Rollback();
                    Assert.Fail(pe.Message);
                }
            }
        }
    }
}
